package com.cloudbroker.bcs.platform.ltb.middle.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by wangxu13798 on 2016/3/17.
 */
public class MD5Util {

    private static Logger logger = LoggerFactory.getLogger(MD5Util.class);



    public static String dictSort(Map<String,String> paramsMap){
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
            if(StringUtils.isNotBlank(entry.getKey()) && StringUtils.isNotBlank(entry.getValue())) {
                sb.append( entry.getKey()).append("=") .append( entry.getValue() ).append("&");
            }
        }
        logger.info(sb.toString()+"\r\n");
        return sb.toString();
    }

    public static String encrypt(String pw){
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            byte[] inputByteArray = pw.getBytes();
            messageDigest.update(inputByteArray);
            byte[] resultByteArray = messageDigest.digest();
            logger.info(byteArrayToHex(resultByteArray)+"\r\n");
            return byteArrayToHex(resultByteArray);
        }catch (NoSuchAlgorithmException e){
            e.printStackTrace();
        }
        return null;
    }

    public static String byteArrayToHex(byte[] byteArray){
        char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        char[] resultCharArray = new char[byteArray.length*2];
        int index = 0;
        for (byte b : byteArray){
            resultCharArray[index++] = hexDigits[b>>4 & 0xf];
            resultCharArray[index++] = hexDigits[b&0xf];
        }
        return new String(resultCharArray);
    }

    public static void transMap2Bean(Map<String, Object> map, Object obj) {

        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                if (map.containsKey(key)) {
                    Object value = map.get(key);
                    // 得到property对应的setter方法
                    Method setter = property.getWriteMethod();
                    setter.invoke(obj, value);
                }

            }

        } catch (Exception e) {
            System.out.println("transMap2Bean Error " + e);
        }

        return;

    }

//    public static void main(String[] args){
//        TreeMap<String,String> treeMap = new TreeMap<>();
//        treeMap.put("appid","wx59cf2033584cf0dc");
//        treeMap.put("attach","wxtest");
//        treeMap.put("body","JSAPItest");
//        treeMap.put("mch_id","1322680501");
//        treeMap.put("nonce_str","1add1a30ac87aa2db72f57a2375d8fec");
//        treeMap.put("notify_url","http://wxpay.weixin.qq.com/pub_v2/pay/notify.v2.php");
//        treeMap.put("openid","oR6Pdvk_2vTggVnLUFrzqcBr4nwY");
//        treeMap.put("out_trade_no","1415659995");
//        treeMap.put("spbill_create_ip","14.23.150.212");
//        treeMap.put("total_fee","1");
//        treeMap.put("trade_type","JSAPI");
//        String str = dictSort(treeMap);
//        System.out.println(str);
//        str = encrypt(str);
//        System.out.println(str);
//        treeMap.put("sign",str);
//        PayMessage payMessage = new PayMessage();
//        transMap2Bean((Map)treeMap,payMessage);
//        payMessage.setAppid(treeMap.get("appid"));
//        payMessage.setAttach(treeMap.get("attach"));
//        payMessage.setBody(treeMap.get("body"));
//        payMessage.setMch_id(treeMap.get("mch_id"));
//        payMessage.setNonce_str(treeMap.get("nonce_str"));
//        payMessage.setNotify_url(treeMap.get("notify_url"));
//        payMessage.setOpenid(treeMap.get("openid"));
//        payMessage.setOut_trade_no(treeMap.get("out_trade_no"));
//        payMessage.setSpbill_create_ip(treeMap.get("spbill_create_ip"));
//        payMessage.setTotal_fee(treeMap.get("total_fee"));
//        payMessage.setTrade_type(treeMap.get("trade_type"));
//        payMessage.setSign(str);
//        System.out.println(XmlUtil.textToXml(payMessage));
//        String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
//        String str1 = XmlUtil.textToXml(payMessage);
//        str1 = str1.replaceAll("__","_");
//        System.out.println(str1);
//        String res = HttpUtil.doXmlPost(url,str1);
//        System.out.println(res);
//        System.out.println(XmlUtil.xmltoMap(res));
//        Map<String,String> resMap = XmlUtil.xmltoMap(res);
//        TreeMap<String,String> payMap = new TreeMap<>();
//        payMap.put("appId",resMap.get("appid"));
//        payMap.put("timeStamp",(System.currentTimeMillis()/1000+""));
//        payMap.put("nonceStr","adssdasssd13d");
//        payMap.put("package","prepay_id="+resMap.get("prepay_id"));
//        payMap.put("signType","MD5");
//        str = dictSort(payMap);
//        str = encrypt(str);
//        payMap.put("paySign",str);
//        System.out.println(payMap);
//    }
}
